package com.tianma315.admin.device.controller;


import java.util.Arrays;
import java.util.Calendar;

import com.tianma315.admin.common.base.BaseController;
import com.tianma315.admin.common.utils.Result;
import com.tianma315.admin.device.vo.MaintainRecordAndTypeVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.device.domain.MaintainRecordDO;
import com.tianma315.admin.device.service.MaintainRecordService;

/**
 * 
 * <pre>
 * 设备维护记录表
 * </pre>
 * <small> 2019-05-22 16:08:48 | Aron</small>
 */
@Controller
@RequestMapping("/device/maintainRecord")
public class MaintainRecordController extends BaseController{
	@Autowired
	private MaintainRecordService maintainRecordService;
	
	@GetMapping()
	@RequiresPermissions("device:maintainRecord:maintainRecord")
	String MaintainRecord(){
	    return "device/maintainRecord/maintainRecord";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("device:maintainRecord:maintainRecord")
	public Result<Page<MaintainRecordAndTypeVO>> list(Integer pageNumber, Integer pageSize, MaintainRecordDO maintainRecordDTO){
		Page<MaintainRecordAndTypeVO> page = maintainRecordService.getMaintainRecordPage(pageNumber,pageSize,maintainRecordDTO);
        return Result.ok(page);
	}
	
	@GetMapping("/add/{deviceId}")
	@RequiresPermissions("device:maintainRecord:add")
	String add(@PathVariable("deviceId")Integer deviceId,Model model){
		model.addAttribute("deviceId",deviceId);
	    return "device/maintainRecord/add";
	}

	@GetMapping("/edit/{maintainRecordId}")
	@RequiresPermissions("device:maintainRecord:edit")
	String edit(@PathVariable("maintainRecordId") Integer maintainRecordId,Model model){
		MaintainRecordDO maintainRecord = maintainRecordService.selectById(maintainRecordId);
		model.addAttribute("maintainRecord", maintainRecord);
	    return "device/maintainRecord/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("device:maintainRecord:add")
	public Result<String> save( MaintainRecordDO maintainRecord){
		maintainRecord.setCreateDate(Calendar.getInstance().getTime());
		maintainRecord.setCreateBy(getUserId());
		if(maintainRecordService.addMaintainRecord(maintainRecord)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("device:maintainRecord:edit")
	public Result<String>  update( MaintainRecordDO maintainRecord){
		maintainRecordService.updateById(maintainRecord);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("device:maintainRecord:remove")
	public Result<String>  remove( Integer maintainRecordId){
		if(maintainRecordService.deleteMaintainRecord(maintainRecordId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("device:maintainRecord:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] maintainRecordIds){
		for (int i = 0; i < maintainRecordIds.length; i++) {
			maintainRecordService.deleteMaintainRecord(maintainRecordIds[i]);
		}
		return Result.ok();
	}
	
}
